<template>
  <div class="login">
    <el-card style="width: 400px;height: 400px;margin-left:600px">
      <template #header>
        <div class="card-header">


        </div>
      </template>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm"
        :size="formSize" status-icon>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="用户密码" prop="pwd">
          <el-input v-model="ruleForm.pwd" type="password" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="btn">
          <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
          <el-button type="primary">注册</el-button>
        </div>
      </template>
    </el-card>
  </div>
</template>
  
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Login } from '@/api/login/index'
import { type LoginData } from '@/api/login/type'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'


const router = useRouter()
const route = useRoute()

// interface RuleForm {
//   userName: string
//   userPass: string
// }
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<LoginData>({
  name: '',
  pwd: '',
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<LoginData>>({
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      login()
    }
  })
}
const login = async () => {
  await Login(ruleForm).then(function (res) {
    console.log(res.data.code);
    if (res.data.code == -1) {
      ElMessage.error(res.data.message)
    }
    if (res.data.code == -2) {
      ElMessage.error(res.data.message)
    }
    if (res.data.code == 1) {
      ElMessage.success(res.data.message)
      localStorage.setItem('tenant_id', res.data.user.tenant_id)
      localStorage.setItem('isAuthenticated', 'true')
      localStorage.setItem('token', res.data.authInfo)
      console.log(res.data.roleId);
      localStorage.setItem('roleId', res.data.roleId)
      router.push({ name: "MainView" })
    }

  })
}
</script>
  
<style>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url(https://oss.mubyte.cn/wp/2022/02/2022022208085555.png);
  background-repeat: no-repeat;
}

.card-header {
  display: flex;
  justify-content: space-between;
}

.btn {
  display: flex;
  justify-content: center;
}

.btn button {
  margin: 0 10px;
}
</style>